<?php
//客户列表
namespace app\ausadmin\controller;
use app\ausadmin\controller\Common;
use think\Request;
use think\Db;
use think\Controller;
use think\Config;
use Godok\Org\Auth;
/**
 *
 */
class Customer extends Common
{

    public function index(){
        $pages = 20;
        $where = 'is_delete = 1';
        $pageParam    = ['query' =>[]];
        //判断是否有提交数据
        if(!empty(input('mobile'))){
            $where .= ' AND mobile = '.input('mobile');
            $pageParam['query']['mobile'] = input('mobile');
            $this->assign('mobile',input('mobile'));
        }
        if(!empty(input('name'))){
            $where .= ' AND name LIKE '."'%".input('name')."%' ";
            $pageParam['query']['name'] = input('name');
            $this->assign('name',input('name'));
        }
        $list=Db::name('user')
            ->where($where)
            ->order('create_time desc')
            ->paginate($pages, false, $pageParam);
        $page = $list->render();
        $listArr = [];
        foreach ($list  as $k=>$v) {
            $balance = Db::name('rechargeorder')->where('uid',$v['id'])->where(['is_delete'=>1,'pay_status'=>1])->where('balance','>',0)->count();
            $deposit = Db::name('rechargeorder')->where('uid',$v['id'])->where(['is_delete'=>1,'pay_status'=>1])->where('deposit','>',0)->count();
            if($balance){
                $v['cbalance'] = $balance;
            }else{
                $v['cbalance'] = '';
            }
            if($deposit){
                $v['cdeposit'] = $deposit;
            }else{
                $v['cdeposit'] = '';
            }
            if($v['balance'] == 0 && $v['deposit'] == 0 && empty($v['name']) && empty($v['mobile'])) {
                continue;
            }else{
                $listArr[$k] = $v;
            }
        }
// 		dump($listArr);die;
        $this->assign('list',$listArr);
        $this->assign('page',$page);
        return $this->fetch();
    }

    public function refund(){
        $pages = 20;
        $where = 'a.is_delete = 1';
        $pageParam    = ['query' =>[]];
        if(!empty(input('order_no'))){
            $where .= ' AND a.order_no = '.input('order_no');
            $pageParam['query']['order_no'] = input('order_no');
        }
        if(input('name')!=''){
            $where .= ' AND b.name LIKE '."'%".input('name')."%' ";
            $pageParam['query']['name'] = input('name');
        }
        $b = 'user';
        $list=Db::name('refund')
            ->alias('a')
            ->join("$b b",'a.oid = b.oid and a.uid = b.id')
            ->field('a.id,a.order_no,a.refund_no,a.transaction_id,a.refund_time,b.mobile,b.name,b.picture')
            ->where($where)
            ->paginate($pages, false, $pageParam);
        $this->assign('list',$list);
        return $this->fetch('../application/publicview/customer/refund.html');
    }

    public function delete($id=''){
        $id =  Request::instance()->get('id','','number_int');
        if($id){
            $data['id'] = $id;
            $data['is_delete'] =0;
            if(Db::name( 'user')->update($data)){
                //$this->redirect('index');
                $oid = 1;$m = 'ausadmin'; $c = 'Customer'; $action="delete"; $uid=session('adminid'); $querystring = '删除客户信息'.$id;
                $ip = get_ip();$os = get_os();
                set_logs($oid,$m,$c,$action,$uid,$querystring,$ip,$os);
                return ['code'=>0, 'msg'=>lang('删除成功！')];
            }else{
                return ['code'=>-1050, 'msg'=>lang('删除失败！')];
            }

        }
    }

    public function edit($id='',$data=""){

        $id = Request::instance()->get('id','','number_int');
        $where['id'] = $id;
        $userinfo = Db::name('user')->where($where)->find();

        $leaseorder = DB::name('leaseorder')->where('uid',$id)->order('create_time desc')->limit(10)->select();
        if ( Request::instance()->isPost() ) {
            //$updata = input('post.');
            $updata['id'] = input('id');
            $updata['name'] = input('name');
            $updata['mobile'] = input('mobile');
            $updata['gender'] = input('gender');
            $updata['province'] = input('province');
            $updata['city'] = input('city');
            $save=Db::name( 'user')->update($updata);
            if($save){
                // $this->success('修改成功！',url('index'));
                $oid = 1;$m = 'ausadmin'; $c = 'Customer'; $action="edit"; $uid=session('adminid'); $querystring = '更新客户信息'.$id;
                $ip = get_ip();$os = get_os();
                set_logs($oid,$m,$c,$action,$uid,$querystring,$ip,$os);
                return ['code'=>0, 'msg'=>lang('修改成功！')];
                //$this->redirect('index');
            }else{
                $this->error(lang('修改失败！'));
            }
            return;

        }
        $this->assign('v',$userinfo);
        $this->assign('list',$leaseorder);
        return $this->fetch('../application/publicview/customer/edit.html');
    }

    public function depositRefund($id="",$deposit=''){
        /*$data= input('post.');
        var_dump($data); die;*/
        echo 123456;
        die;

//yue退款
    }
    public function balance(){
        $id = Request::instance()->get('id','','number_int');
        $info = Db::name('user')->where('id',$id)->find();
        $this->assign('v',$info);
        if(!empty($id)){
            $list = Db::name('rechargeorder')
                ->alias('a')
                ->join('user b','a.uid = b.id')
                ->where('a.uid',$id)
                ->where('a.is_delete',1)
                ->where('a.balance','>',0)
                ->where('a.deposit',0)
                ->where('a.refundbalance',null)
                ->where('a.pay_status',1)
                ->order('a.create_time desc')
                ->field('a.*,b.name')
                ->select();
            $this->assign('list',$list);
            //计算总余额
            $sum = Db::name('rechargeorder')
                ->where(['uid'=>$id,'is_delete'=>1,'pay_status'=>1])
                ->where('balance','>',0)
                ->where('deposit',0)
                ->where('refundbalance',null)
                ->where('refunddeposit',null)
                ->sum('real_amount');
            $this->assign('sum',$sum);
            return view('../application/publicview/customer/balance.html');
            //var_dump($sum);die;


        }
    }
    public function deposit(){
        $id = Request::instance()->get('id','','number_int');
        if(!empty($id)){
            $where['id'] = $id;
            $userinfo = Db::name('user')->where($where)->find();
            $this->assign('v',$userinfo);
            return view('../application/publicview/customer/deposit.html');
        }
        if ( Request::instance()->isPost() ) {
            $data = input('post.');
            $data['id'] = (int)input('id');
            if($data['id']){
                $save=Db::name( 'user')->update($data);
                if($save !==false){
                    $userinfo = Db::name('user')->where('id',$data['id'])->find();
                    $oid = 1;$m = 'ausadmin'; $c = 'Customer'; $action="deposit"; $uid=session('adminid'); $querystring = '添加客户：'.$userinfo['name'].' 押金金额'.$data['deposit'];
                    $ip = get_ip();$os = get_os();
                    set_logs($oid,$m,$c,$action,$uid,$querystring,$ip,$os);
                    return ['code'=>0, 'msg'=>lang('押金设置成功！')];
                }else{
                    return ['code'=>-1050, 'msg'=>lang('押金设置失败！')];
                }
            }else{
                die;
            }
        }

    }
}
